package model;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.function.Function;
import java.util.stream.Collectors;

/**
 * @author gubengang
 * @create 2021-09-30 19:21
 */
public class Resource {
    private Map<Integer,Long> hostId2HoldMap = new HashMap<>();
    private List<Host> hosts = new ArrayList<Host>(6);
    private Map<Integer,Host> hostId2HostMap;

    public Resource(){
        for(int i=0; i<6;i++){
            hosts.add(new Host(i));
        }
        hostId2HostMap = hosts.stream().collect(Collectors.toMap(Host::getHostId, Function.identity()));
    }

    public List<Host> getHosts() {
        return hosts;
    }

    public Host getHostByHostId(Integer hostId){
        return hostId2HostMap.get(hostId);
    }

    public Long getSizeByHostId(Integer hostId){
        return hostId2HoldMap.getOrDefault(hostId,0L);
    }

    public void addBlock2Host(Integer hostId, Long size){
        hostId2HoldMap.put(hostId,getSizeByHostId(hostId)+size);
    }

    public void setHosts(List<Host> hosts) {
        this.hosts = hosts;
    }
}
